package com.ruoyi.quartz.task;

import com.ruoyi.system.domain.HikVideoAllGroupFlowNew;
import com.ruoyi.system.domain.Income;
import com.ruoyi.system.service.IHikVideoAllGroupFlowNewService;
import com.ruoyi.system.service.IIncomeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component("incomeTask")
@Slf4j
public class IncomeTask {


    @Autowired
    private IIncomeService incomeService;

    @Autowired
    private IHikVideoAllGroupFlowNewService hikVideoAllGroupFlowNewService;

    public void insertIncome() {
        System.out.println("开始执行收入定时任务----------------------");
        //获取当前时间
        System.out.println("当前时间：" + System.currentTimeMillis());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        int hours = date.getHours();
        String dateStr = format.format(date);
        System.out.println("当前时间：" + dateStr);
        Income income = incomeService.selectIncomeByAddTime(dateStr);
        if (income == null){
            //添加一条数据到表中
            Income income1 = new Income();
            income1.setCreateTime(date);
            income1.setCount(0L);
            income1.setUpdataTime(date);
            incomeService.insertIncome(income1);
            log.info("添加一条数据到表中:{}", income1);
        }
        //如何是1-7点，不更新数据，如果是7-23点，则更新数据
        if (hours >= 0 && hours <= 23 && income != null) {
            //查询当当天的数据
            Long count = income.getCount();
            System.out.println("count:" + count);
            //创建一个30-100的随机数
            int random = (int) (Math.random() * 500 + 200);
            System.out.println("random:" + random);
            count += random;
            income.setCount(count);
            income.setUpdataTime(date);
            incomeService.updateIncome(income);
            //更新数据
            System.out.println("更新数据");
        }
        System.out.println("结束执行客流任务----------------------");
    }
}
